$(function(){
    loginName=baseFunction.getUrlParameterValueByKey("loginName");
    id=baseFunction.getUrlParameterValueByKey("id"); //当前选中信用卡的id
    aisle=baseFunction.getUrlParameterValueByKey("aisle"); //通道编号
    accountBalance=baseFunction.getUrlParameterValueByKey("accountBalance"); //账户余额
    addRate=baseFunction.getUrlParameterValueByKey("addRate"); //额外增加费率
    rmentMode=baseFunction.getUrlParameterValueByKey("rmentMode"); 
    ruleCode=baseFunction.getUrlParameterValueByKey("ruleCode"); 

    pageData =JSON.parse(decodeURIComponent(baseFunction.getUrlParameterValueByKey("data"))); //页面数据
    var  rments = pageData.rments;
    var consumes = pageData.consumes; 
    pageList=[]; //页面列表数据
    for (var i=0;i<rments.length;i++){
        pageList[i] = [].concat(rments[i]);
      for(var j=0;j<consumes.length;j++){
        if (rments[i].orderno == consumes[j].rmentOrderno){
            pageList[i] = pageList[i].concat(consumes[j]);
        }
      }
    }
    // 提交
    $("#submit").on("click",function(){
        submit();
    })

    init(); //初始化

})

/**
 * 页面初始化
 */
function init(){
    $(".head .view1").html("计划单号："+pageData.planBean.planOrderno);//订单号
    $(".head .view2").html("还款周期："+pageData.planBean.rmentDate);//还款周期
    $(".repayment .left .money").html("￥"+pageData.planBean.planAmount);//还款金额
    $(".repayment .right .money").html("￥"+pageData.planBean.planFee);//手续费
    // 列表
    var html="";
    console.log(pageList);
    pageList.forEach(function(key,index){
        key.forEach(function(key,index){
            if(index==0){
                html+="<div class='top'>"
                +"<img src='../static/img/bankcard_icon_forward.png'>"
                +"<text>"+key.rmentDate+"</text>"
                +"<div class='cen'>还款汇入</div>"
                +"<text class='fr right'>+"+key.amount+"</text>"
                +"</div>"
            }else{
                html+="<div class='li'><div>"
                +"<text>"+key.consumeDate+" "+key.consumeTime+"</text>"
                +"<div class='cen'>消费支出</div>"
                +"<text class='fr green'>-"+key.amount+"</text>"                  
                +"</div></div>"
            }
        })  
    });
    $(".content .list").html(html);
}

  /**
   * 提交
   */
function submit(){
    // 判断账户余额 有可以继续下单  没有提示去充值
    if (!(Number(accountBalance) >= Number(pageData.planBean.planAmount) + Number(pageData.planBean.planFee))){
        $(".mask .tip").html("余额不足!是否立即去充值?");
        $(".mask").show();
        var btn=document.querySelector(".mask .btn");
        var mask=document.querySelector(".mask");
        btn.onclick=function(){
            $(".mask").hide();
            window.location.href='index_recharge.html?loginName=' + loginName + '&aisle=' + aisle + ' &addRate=' + addRate + ' &rmentMode=' + rmentMode + ' &ruleCode=' + ruleCode +'&isReloadCard=true&id='+id+'';
        }
        return;
    }
    // 下单
    $(".loding").show();
    var  data={
      userName: baseFunction.encryption(loginName),
    };
    baseFunction.netRequest("creditPlanOrder",data,function(res){
        $(".loding").show();
        
        //console.log(res);
        if(res.code==200){
            $(".payPassword").removeClass("hide").addClass("show");
            $(".payPassword #confPay").on("click",function(){ //确认提交

            })
        }
    })
}

/**
 * 确认定制
 */
function confPay(){
    $(".payPassword").removeClass("show").addClass("hide");
    $(".loding").show();
    //1 验证密码是否正确
    var data = {
        userName: baseFunction.encryption(loginName),
        password: baseFunction.encryption(hexMD5(this.data.password)),
    };
    baseFunction.netRequest("validPayPwd", data, function (res) {
        
        if (res.code == 200) { //密码正确
            // 2确认制作还款计划
            var data = {
                userName: baseFunction.encryption(loginName),
                password: baseFunction.encryption(hexMD5($(".payPassword .password").val())),
                planOrderno: pageData.planBean.planOrderno
            };
            baseFunction.netRequest("creditPlanPay", data, function (res) {
                $(".loding").hide();
                
                $(".prompt").html(res.message).show(0).delay(1000).hide(0);
                if(res.code==200){ //制作成功
                    window.location.href='index_planSuccessful.html?planOrderno=' + pageData.planBean.planOrderno+'';
                }
            })
        }else{ //密码错误
            $(".prompt").html(res.message).show(0).delay(2000).hide(0);
        }
    })
}